package com.sky.controller.admin;

import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController("adminShopStatus")
@Api(tags = "店铺相关接口")
@RequestMapping("/admin/shop")
public class ShopController {
    private final String key = "SHOP_STATUS";

    @Autowired
    RedisTemplate<String, Object> redisTemplate;

    @PutMapping("/{status}")
    @ApiOperation("修改店铺状态")
    public Result setStatus(@PathVariable Integer status) {
        log.info("修改店铺状态:{}", status == 1 ? "营业中" : "打烊了");
        ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
        valueOperations.set(key, status);
        return Result.success();
    }

    @ApiOperation("获取店铺状态")
    @GetMapping("/status")
    public Result<Integer> getStatus() {
        log.info("获取店铺状态");
        ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
        Integer status = (Integer) valueOperations.get(key);
        if (status != null) {
            return Result.success(status);
        }
        return Result.error("获取店铺状态失败");
    }
}
